//
//  ADStatusCell.swift
//  myBlog
//
//  Created by mac2014 on 16/3/28.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit
import SnapKit
//整个微博的cell
class ADStatusCell: UITableViewCell {
    //MARK: - 属性
    var bottomTopCons: Constraint?
    var status:ADStatus? {
        didSet {
            //将原创的微博模型传进来
            originalView.status = status
            
            //转发微博
            bottomTopCons?.uninstall()
            //print("bottomTopCons:\(bottomTopCons)")
            if let retweetedStatus = status?.retweeted_status{
                //有转发
                retweetView.retweetStatus = retweetedStatus
                //print("haha:\(retweetView.retweetStatus)")
                retweetView.hidden = false
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                //没有转发
                retweetView.hidden = true
                bottomView.snp_makeConstraints { (make) -> Void in
                    self.bottomTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                }
            }
            
//            print("\(self.bottomTopCons)")
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reusableIdentifier)
        prepareUI()
    }
    
    //MARK: - 自定义方法
    func prepareUI() {
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetView)

        //约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
//            make.height.equalTo(300)
        }
        bottomView.snp_makeConstraints { (make) -> Void in
            self.bottomTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
        }
        
        //cell自适应内容的高度，需要对contentView进行约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
    }
    
    //MARK: - 懒加载
    private lazy var originalView: ADOriginalView = ADOriginalView()
    private lazy var bottomView: ADStatusBottomView = ADStatusBottomView()
    private lazy var retweetView: ADRetweetView = ADRetweetView()

}
